package com.xca.znb.view;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.xca.znb.R;
import com.xca.znb.entity.Local;
import com.xca.znb.model.impl.LocalManagerImpl;
import com.xca.znb.presenter.LocalManagerPresenter;

import java.util.List;

public class LocalManagerActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageView Back ;
    private ListView listLocal ;
    private Button AddLocal ;
    private List<Local> localList ;
    private ArrayAdapter<Local> adapter ;
    private LocalManagerPresenter presenter ;
    private boolean isDataUpdated = false;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_local_manager);
        Back = findViewById(R.id.my_local_manager_back) ;
        listLocal = findViewById(R.id.my_local_list) ;
        AddLocal = findViewById(R.id.add_local_btn) ;
        Back.setOnClickListener(this);
        AddLocal.setOnClickListener(this);

        LocalManagerImpl localManager = new LocalManagerImpl(this);
        presenter = new LocalManagerPresenter(localManager);

        SharedPreferences sharedPreferences = getSharedPreferences("loginPhone", Context.MODE_PRIVATE);
        String phone = sharedPreferences.getString("phone" , "");

        localList = presenter.getLocalByPhone(phone);
        adapter = new ArrayAdapter<Local>(this , R.layout.local_list_item , R.id.localList_show_local , localList){
            public View getView(int position , View converView , ViewGroup parent){
                View view = super.getView(position , converView , parent);
                TextView showLocal = view.findViewById(R.id.localList_show_local);
                TextView showId = view.findViewById(R.id.localList_show_id) ;
                TextView showName = view.findViewById(R.id.localList_show_name);
                TextView showPhone = view.findViewById(R.id.localList_show_phone) ;
                TextView showDefault = view.findViewById(R.id.localList_show_default) ;
                ImageButton goUpdate = view.findViewById(R.id.localList_update_btn);
                Local local = getItem(position);
                if (local != null) {
                    showLocal.setText(local.getAddress());
                    showId.setText(String.valueOf(local.getId()));
                    showName.setText(local.getName());
                    showPhone.setText(local.getPhone());
                    if (local.getType().equals("是")){
                        showDefault.setText("默认");
                    }
                    goUpdate.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(LocalManagerActivity.this , UpdateLocalActivity.class);
                            intent.putExtra("myLocalId" , local.getId());
                            startActivity(intent);
                            finish();
                        }
                    });
                }


                return view ;
            }
            public void add(Local local){
                localList.add(local);
                notifyDataSetChanged();
            }
            public void remove(Local local){
                localList.remove(local);
                notifyDataSetChanged();
            }
            public void update(int position , Local local){
                localList.set(position,local);
                notifyDataSetChanged();
            }
        };
        listLocal.setAdapter(adapter);

    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.my_local_manager_back){
            finish();
        } else if (v.getId() == R.id.add_local_btn) {
            Intent intent = new Intent(this , AddNewLocalActivity.class) ;
            startActivity(intent);
            finish();
        }
    }
}
